<?php
namespace Admin\Controller;
use Admin\Model\ArticleModel;
use Frame\Libs\BaseController;

class ArticleController extends BaseController {
    public function article_manage() {
        $model=ArticleModel::getInstance();
        $articles=$model->fetchAll();
        $this->smarty->assign('articles',$articles);
        $this->smarty->display('index/article_manage.tpl');
    }

    // 获取单篇文章数据（用于编辑）
    public function getArticle() {
        // 验证是否为AJAX请求
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            echo json_encode(array('success' => false, 'message' => '非法请求'));
            exit;
        }

        $articleId = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if($articleId <= 0) {
            echo json_encode(array('success' => false, 'message' => '参数错误'));
            exit;
        }

        $model = ArticleModel::getInstance();
        $article = $model->fetchOne($articleId);
        if($article) {
            echo json_encode(array('success' => true, 'data' => $article));
        } else {
            echo json_encode(array('success' => false, 'message' => '文章不存在'));
        }
        exit;
    }

    // 添加文章
    public function addArticle() {
        // 验证是否为AJAX请求
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            echo json_encode(array('success' => false, 'message' => '非法请求'));
            exit;
        }

        // 获取表单数据
        $title = isset($_POST['title']) ? trim($_POST['title']) : '';
        $author = isset($_POST['author']) ? trim($_POST['author']) : '';
        $category = isset($_POST['category']) ? trim($_POST['category']) : '';
        $content = isset($_POST['content']) ? trim($_POST['content']) : '';
        $status = isset($_POST['status']) ? intval($_POST['status']) : 0;

        // 简单验证
        if(empty($title) || empty($author) || empty($category) || empty($content)) {
            echo json_encode(array('success' => false, 'message' => '请填写必填字段'));
            exit;
        }

        // 准备插入数据
        $data = array(
            'title' => $title,
            'author' => $author,
            'category' => $category,
            'content' => $content,
            'status' => $status,
            'last_time' => date('Y-m-d H:i:s')
        );

        $model = ArticleModel::getInstance();
        if($model->insert($data)) {
            echo json_encode(array('success' => true, 'message' => '文章添加成功'));
        } else {
            echo json_encode(array('success' => false, 'message' => '文章添加失败'));
        }
        exit;
    }

    // 更新文章
    public function updateArticle() {
        // 验证是否为AJAX请求
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            echo json_encode(array('success' => false, 'message' => '非法请求'));
            exit;
        }

        // 获取表单数据
        $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
        $title = isset($_POST['title']) ? trim($_POST['title']) : '';
        $author = isset($_POST['author']) ? trim($_POST['author']) : '';
        $category = isset($_POST['category']) ? trim($_POST['category']) : '';
        $content = isset($_POST['content']) ? trim($_POST['content']) : '';
        $status = isset($_POST['status']) ? intval($_POST['status']) : 0;

        // 简单验证
        if($id <= 0 || empty($title) || empty($author) || empty($category) || empty($content)) {
            echo json_encode(array('success' => false, 'message' => '请填写必填字段'));
            exit;
        }

        // 准备更新数据
        $data = array(
            'title' => $title,
            'author' => $author,
            'category' => $category,
            'content' => $content,
            'status' => $status,
            'last_time' => date('Y-m-d H:i:s')
        );

        $model = ArticleModel::getInstance();
        if($model->update($data, $id)) {
            echo json_encode(array('success' => true, 'message' => '文章更新成功'));
        } else {
            echo json_encode(array('success' => false, 'message' => '文章更新失败'));
        }
        exit;
    }

    // 删除文章
    public function deleteArticle() {
        // 验证是否为AJAX请求
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            echo json_encode(array('success' => false, 'message' => '非法请求'));
            exit;
        }

        $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
        if($id <= 0) {
            echo json_encode(array('success' => false, 'message' => '参数错误'));
            exit;
        }

        $model = ArticleModel::getInstance();
        if($model->delete($id)) {
            echo json_encode(array('success' => true, 'message' => '文章删除成功'));
        } else {
            echo json_encode(array('success' => false, 'message' => '文章删除失败'));
        }
        exit;
    }

    // 批量删除文章
    public function batchDeleteArticles() {
        // 验证是否为AJAX请求
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            echo json_encode(array('success' => false, 'message' => '非法请求'));
            exit;
        }

        $ids = isset($_POST['ids']) ? trim($_POST['ids']) : '';
        if(empty($ids)) {
            echo json_encode(array('success' => false, 'message' => '请选择要删除的文章'));
            exit;
        }

        // 转换为ID数组并验证
        $idArray = array_map('intval', explode(',', $ids));
        $validIds = array_filter($idArray, function($id) { return $id > 0; });
        
        if(empty($validIds)) {
            echo json_encode(array('success' => false, 'message' => '参数错误'));
            exit;
        }

        $model = ArticleModel::getInstance();
        $deletedCount = $model->batchDelete($validIds);
        
        if($deletedCount > 0) {
            echo json_encode(array('success' => true, 'message' => '批量删除成功', 'deletedCount' => $deletedCount));
        } else {
            echo json_encode(array('success' => false, 'message' => '批量删除失败'));
        }
        exit;
    }

    // 搜索文章
    public function searchArticles() {
        // 验证是否为AJAX请求
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            echo json_encode(array('success' => false, 'message' => '非法请求'));
            exit;
        }

        $keyword = isset($_POST['keyword']) ? trim($_POST['keyword']) : '';
        if(empty($keyword)) {
            echo json_encode(array('success' => false, 'message' => '请输入搜索关键词'));
            exit;
        }

        $model = ArticleModel::getInstance();
        $articles = $model->search($keyword);
        
        echo json_encode(array('success' => true, 'data' => $articles));
        exit;
    }

}
